# -*- coding: utf-8 -*-
__author__ = 'ooo'
__date__ = ' 11:55'

# Create your models here.

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Q, Sum
from django.http import HttpResponseRedirect,JsonResponse
from django.contrib.auth.backends import ModelBackend
from django.utils import timezone
from utils.flat_uuid import FlatUUID, ShortUUID8
from utils.object_none import get_object_or_none
from utils.check_time_span import date_ago
from django.core.urlresolvers import reverse
from uuslug import uuslug


# setting 里要有对应的配置
class CustomBackend(ModelBackend):
    def authenticate(self, username=None, password=None, **kwargs):
        try:
            user = Auser.objects.get(models.Q(username = username) | models.Q(email = username))
            if user.check_password(password):
                return user
        except Exception as e:
            return None


class Auser(AbstractUser):
    # 个人信息（补充）
    id = models.CharField('主键', primary_key=True, auto_created=True, default=ShortUUID8, editable=False, max_length=50)
    nickname = models.CharField('个人昵称', null=True, unique=True, max_length=20)
    avatar = models.CharField('个人头像', max_length=252, default='')
    pphone = models.IntegerField('手机号码', null=True, blank=True)

    def __str__(self):
        return str(self.nickname)

    def save(self, *args, **kwargs):
        if not self.email:
            self.email = self.username
        super(Auser,self).save(*args, **kwargs)

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
